<!--
 * @Author: Bennty
 * @Date: 2024-08-27 12:50:13
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-10-30 10:03:15
 * @Description: 请填写简介
-->
<template>
    <el-dialog v-model="props.visible" title="出库" @close="handleClose" width="20%" destroy-on-close>
        <el-form :model="formData" ref="formRef">
            <el-form-item label="材料大类">
                <span>{{ props.data.productCategoriesName }}</span>
            </el-form-item>
            <el-form-item label="材料小类">
                <span>{{ props.data.productSubcategoryName }}</span>
            </el-form-item>
            <el-form-item label="规格">
                <span>{{ props.data.specsDetail }}</span>
            </el-form-item>
            <el-form-item label="当前库存">
                <span>{{ `${props.data.currentInventory}个` }}</span>
            </el-form-item>
            <el-form-item label="领用权限" prop="applyPermission">
                <el-tree-select 
                    v-model="formData.applyPermission" 
                    :data="props.depts" 
                    value-key="deptId"
                    :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
                    placeholder="请选择"
                >
                    
                </el-tree-select>
            </el-form-item>
            <el-form-item label="领用人" prop="receiver">
                <el-select v-model="applyer" placeholder="请选择" value-key="userId">
                    <el-option v-for="user in props.users" :key="user.userId" :label="user.userName" :value="user" ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="出库日期" prop="outDate">
                <el-date-picker v-model="formData.outDate" placeholder="年/月/日" format="YYYY/MM/DD" value-format="YYYY-MM-DD"></el-date-picker>
            </el-form-item>
            <el-form-item label="出库数量" prop="number">
                <el-row>
                    <el-col :span="24">
                        <el-input v-model="formData.number" placeholder="请输入">
                            <template #append>
                                个
                            </template>
                        </el-input>
                    </el-col>
                </el-row>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark" type="textarea" maxlength="125" show-word-limit></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button  type="primary" @click="handleSubmit">确 定</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import { auxiliaryOutStorageApi, getAuxiliaryPurchaseByIdApi } from '@/api/sj/warehouse/auxiliary';

const props = defineProps(['visible', 'data','users','depts']);
const emits = defineEmits(['close','submit']);
const formRef = ref<FormInstance>();
const applyer = ref<any>();
const purchaseOrders = ref<any[]>();

const formData = ref({
    applyPermission: undefined,
    outDate: undefined,
    number: undefined,
    remark: undefined
});

// watch(()=>props.data, async (newData)=>{
    
//     await getAuxiliaryPurchaseByIdApi(newData.id);
// }, {deep:true});


const handleSubmit = async () => {
    const submitData = {
        auxiliaryId: props.data.id,
        productCategoriesType: props.data.productCategoriesType,
        productCategoriesName: props.data.productSubcategoryName,
        productSubcategoryType: props.data.productSubcategoryType,
        productSubcategoryName: props.data.productSubcategoryName,
        specifications: props.data.specsDetail,
        applyName: applyer.value.userName,
        applyId: applyer.value.userId,
        ...formData.value
    };
    const { code } = await auxiliaryOutStorageApi(submitData);
    if(code == 200){
        emits('submit', true);
        handleClose();
    };
};

const handleClose = ()=>{
    emits('close');
    formRef.value?.resetFields();
    applyer.value = undefined;
};
</script>

<style lang="scss" scoped>
.dialog-footer{
    display: flex;
    justify-content: center;
}
</style>